package com.coolgor.axnut.service.mdmis.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.mdmis.PolicyMonitorDao;
import com.coolgor.axnut.dao.mdmis.PolicyMonitorTaskDao;
import com.coolgor.axnut.dto.mdmis.PolicyMonitorExecution;
import com.coolgor.axnut.entity.mdmis.Policy;
import com.coolgor.axnut.entity.mdmis.PolicyMonitor;
import com.coolgor.axnut.enums.mdmis.PolicyMonitorEnum;
import com.coolgor.axnut.enums.mdmis.PolicyMonitorOperationStateEnum;
import com.coolgor.axnut.service.mdmis.PolicyMonitorTaskService;

@Service
public class PolicyMonitorTaskServiceImpl implements PolicyMonitorTaskService {

	@Autowired
	private PolicyMonitorDao policyMonitorDao;
	@Autowired
	private PolicyMonitorTaskDao policyMonitorTaskDao;

	
	@Override
	@Transactional
	public PolicyMonitorExecution markWarmTheSamePhoneOnDay() {
		PolicyMonitorExecution result = new PolicyMonitorExecution() ;
		List<Policy> policyList = policyMonitorTaskDao.queryTheSamePhoneOnDay();
		if (policyList==null || policyList.size()<1) {
			result = new PolicyMonitorExecution();
			result.setCount(0);
			result.setState(PolicyMonitorOperationStateEnum.EMPTY_LIST.getState());
			result.setStateInfo(PolicyMonitorOperationStateEnum.EMPTY_LIST.getStateInfo());
			return result;
		}
		//获取今天日期
		Date today = new Date(System.currentTimeMillis());
		//设置日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Policy policyLast = new Policy(); //上一个
		Policy policyThis = new Policy(); //这一个
		List<String> insureNoList = new ArrayList<String>();
		String warmString = "";
		PolicyMonitor policyMonitor ;
		List<PolicyMonitor> policyMonitorList = new ArrayList<PolicyMonitor>();
		for (int i=0; i<policyList.size(); i++) {
			policyThis = policyList.get(i);
			if (policyThis.getMobilephone().equals(policyLast.getMobilephone())) {				
				insureNoList.add(policyThis.getInsureNo());
				warmString += policyThis.getInsureNo()+"、";
				if ( (i+1) == policyList.size() ) {
					for (String insureNo:insureNoList) {
						//从数据库中获取这张保单的中断信息
						policyMonitor = policyMonitorDao.queryPolicyMonitorByInsuerNo(insureNo);
						if (policyMonitor==null) {
							//如果数据库中没有这个保单的中断信息
							policyMonitor = new PolicyMonitor();
							policyMonitor.setInsureNo(insureNo);
							policyMonitor.setWarnFlag(PolicyMonitorEnum.WARN_REPEAT.getState());
							policyMonitor.setWarnInfo(warmString);
							policyMonitor.setHoldFlag(PolicyMonitorEnum.NORMAL.getState());
							policyMonitor.setCreateTime(new Date());
							policyMonitor.setLastEditTime(new Date());
							//插入中断信息
							policyMonitorDao.insertPolicyMonitor(policyMonitor);
						} else { 
							//如果数据库中已经有了这个保单的中断信息
							policyMonitor.setWarnFlag(PolicyMonitorEnum.WARN_REPEAT.getState());
							policyMonitor.setWarnInfo(policyMonitor.getWarnInfo()+warmString);
							policyMonitor.setLastEditTime(new Date());
							//更新中断信息
							policyMonitorDao.updatePolicyMonitor(policyMonitor);
						}
						policyMonitorList.add(policyMonitor);
					}
				}
			} else { //如果本保单信息的电话号码跟上一个不相同、执行插入数据库操作
				//判断是否已经有保单号码和警告字符串了。
				if (insureNoList.size()>0 && !"".equals(warmString)) {
					for (String insureNo:insureNoList) {
						//从数据库中获取这张保单的中断信息
						policyMonitor = policyMonitorDao.queryPolicyMonitorByInsuerNo(insureNo);
						if (policyMonitor==null) {
							//如果数据库中没有这个保单的中断信息
							policyMonitor = new PolicyMonitor();
							policyMonitor.setInsureNo(insureNo);
							policyMonitor.setWarnFlag(PolicyMonitorEnum.WARN_REPEAT.getState());
							policyMonitor.setWarnInfo(warmString);
							//插入中断信息
							policyMonitorDao.insertPolicyMonitor(policyMonitor);
						} else { 
							//如果数据库中已经有了这个保单的中断信息
							policyMonitor.setWarnFlag(PolicyMonitorEnum.WARN_REPEAT.getState());
							policyMonitor.setWarnInfo(policyMonitor.getWarnInfo()+warmString);
							//更新中断信息
							policyMonitorDao.updatePolicyMonitor(policyMonitor);
						}
						policyMonitorList.add(policyMonitor);
					}
				}
				insureNoList.clear();
				insureNoList.add(policyThis.getInsureNo());
				warmString = "【"+sdf.format(today)+"】"+"相同手机子母单：";
				warmString += policyThis.getInsureNo()+"、";
			}
			policyLast = policyThis;
		}
		result.setState(PolicyMonitorOperationStateEnum.SUCCESS.getState());
		result.setCount(policyMonitorList.size());
		result.setList(policyMonitorList);
		return result;
	}
}
